package com.api.customization.unionpay.market.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.MarketingRefundService;
import com.customization.unionpay.market.service.impl.MarketingRefundServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 *
 * @author sjm
 * @description 营销退款相关
 */
@Path("/market/refund")
public class MarketingRefundController {

    private static final Logger log = LoggerFactory.getLogger(MarketingRefundController.class);

    private MarketingRefundService getService(User user) {
        return ServiceUtil.getService(MarketingRefundServiceImpl.class, user);
    }

    // 修申请退款明细支付状态
    @POST
    @Path("/update/pay/state")
    @Produces(MediaType.APPLICATION_JSON)
    public String externalUpdatePayState(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "修改成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);
                String actAuthCode = parseJson.getString("ACT_AUTH_CODE");
                // 退款单号
                String refundCode = parseJson.getString("REFUND_CODE");
                // 支付状态
                String paymentStatus = parseJson.getString("PAYMENT_STATUS");
                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(refundCode)) {
                    result.put("msg", "退款单号为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(paymentStatus)) {
                    result.put("msg", "支付状态为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(actAuthCode)) {
                    result.put("msg", "授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                getService(user).externalUpdatePayState(actAuthCode, refundCode,paymentStatus);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

    @POST
    @Path("/details/update/pay/state")
    @Produces(MediaType.TEXT_PLAIN)
    public String updatePayState(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "执行成功！");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            Map req = ParamUtil.request2Map(request);
            String ids = req.get("ids") != null ? req.get("ids").toString() : "";
            String state = req.get("stateValue") != null ? req.get("stateValue").toString() :  "";
            if (ids != null && !"".equals(ids)) {
                String resultStr = getService(user).updatePayState(ids,state, user);
                if (resultStr != null) {
                    result.put("msg", resultStr);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                }
            } else {
                result.put("msg", "请选择数据！");
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", e.getMessage());
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }
}
